<?php
    session_start();
    require_once 'php/database.php';
    require_once 'php/photo.php';

	if (isset($_SESSION['username'])){
			$login = true;
	}
	else{
			$login = false;
	}
    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }
?>

<?php
                echo' <table>';
                    echo' <tr>';

                        echo '<td rowspan="2" valign="middle">';
                            echo '<div id="profileinfocontent">';
                                    $user = getUserNamePage();
                                    $hasil = database::query("SELECT * FROM user WHERE Username = '$user'");
                                    $count = 0;

                                    if(mysql_num_rows($hasil)>0){
                                        while ($baris = mysql_fetch_row($hasil)){
                                            $nama = $baris[3];
                                            $email = $baris[4];
                                            $tanggal = $baris[5];

                                            $gambar = photo::getProfPicPath($user);

                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                if(mysql_num_rows($hasil2)>0){
                                                    $baris2 = mysql_fetch_row($hasil2);
                                                    $gambar= 'users/'.$baris2[1].'/'.$baris2[2];
                                                }
                                            }*/

                                            echo '<div id="fotopropic" class="boxy">';
                                                echo '<img src="'.$gambar.'" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100"/>';
                                            echo '</div>';
                                        }
                                    }
                                
                            echo '</div>';
                        echo '</td>';
                        echo '<td rowspan="2" valign="middle" width="400">';
                            echo '<div class="profile_info">';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_name">Name</label>';
                                    echo '<label class="profileinfo_value" id="name">'.$nama.'</label>';
                                echo '</div>';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_email">Email</label>';
                                    echo '<label class="profileinfo_value" id="email">'.$email.'</label>';
                                echo '</div>';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_tanggal_lahir">Date of Birth</label>';
                                    echo '<label class="profileinfo_value" id="tanggal_lahir">'.$tanggal.'</label>';
                                echo '</div>';
                            echo '</div>';
                        echo '</td>';

                        echo' <td>';
                            echo' <div>';
                                echo' <label>Following</label>';


                                    echo '<div>';
                                            //echo '<div  class="followinger">';
                                            echo '<div>';
                                    if (isset($_SESSION['username'])){
                                            $login = true;
                                    }
                                    else{
                                            $login = false;
                                    }
                                    $user = getUserNamePage();

                                    $hasil = database::query("SELECT Username FROM follownet WHERE Follower = '$user'");
                                    $indeks = 0;
                                    $count = 0;
                                    if(mysql_num_rows($hasil)>0){
                                        while($baris = mysql_fetch_row($hasil)){
                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                $baris2 = mysql_fetch_row($hasil2);
                                                $gambar = 'users/'.$baris2[1].'/'.$baris2[2];
                                            }*/
                                            $pemilik = $baris[0];
                                            $gambar = photo::getProfPicPath($pemilik);
                                            $namaArray[$indeks] = $baris[3];
                                            $pemilikArray[$indeks] = $pemilik;
                                            $gambarArray[$indeks] = $gambar;
                                            ++$indeks;
                                            ++$count;
                                        }

                                        for ($j = 0; $j < 3; ++$j){
                                                echo '&nbsp';
                                                echo '<a title="'.$namaArray[$j].'" href="profile.php?un='.$pemilikArray[$j].'"><img class="boxy2" src="'.$gambarArray[$j].'" alt="fol_'.$j.'" width="50" height="50" name="foto_fol_'.$j.'" id="foto_fol_'.$j.'"/></a>';
                                        }

                                                echo '&nbsp';
                                            echo '</div>';
                                        if ($count == 0){
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>&nbsp';
                                        }
                                    }

                                    echo '</div>';
                                    if ($indeks > 3){
                                        //echo '<br/><a href="">See all...</a>';
                                        echo '<br/><a href="javascript:void()" onclick="goToFollowingUser(\''.$user.'\')">See all..<br/></a>';
                                    }

                                echo' <br/>';
                            echo' </div>';
                        echo' </td>';
                    echo' </tr>';
                    echo' <tr>';
                        echo' <td>';
                            echo' <div>';
                                echo' <br/>';
                                echo' <label>Follower</label>';


                                    echo '<div>';
                                            //echo '<div  class="followinger">';
                                            echo '<div>';
                                    if (isset($_SESSION['username'])){
                                            $login = true;
                                    }
                                    else{
                                            $login = false;
                                    }
                                    $user = getUserNamePage();

                                    $hasil = database::query("SELECT Follower FROM follownet WHERE Username = '$user' LIMIT 4 ");
                                    $indeks = 0;
                                    $count = 0;
                                    if(mysql_num_rows($hasil)>0){
                                        while($baris = mysql_fetch_row($hasil)){
                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                $baris2 = mysql_fetch_row($hasil2);
                                                $gambar = 'users/'.$baris2[1].'/'.$baris2[2];
                                            }*/
                                            $pemilik = $baris[0];
                                            $gambar = photo::getProfPicPath($pemilik);
                                            $namaArray2[$indeks] = $baris[3];
                                            $pemilikArray2[$indeks] = $pemilik;
                                            $gambarArray2[$indeks] = $gambar;
                                                
                                            ++$indeks;
                                            ++$count;
                                        }

                                        for ($j = 0; $j < 3; ++$j){
                                            if($j < $indeks){
                                                echo '&nbsp';
                                                echo '<a title="'.$namaArray2[$j].'" href="profile.php?un='.$pemilikArray2[$j].'"><img class="boxy2" src="'.$gambarArray2[$j].'" alt="fol_'.$j.'" width="50" height="50" name="foto_fol_'.$j.'" id="foto_fol_'.$j.'"/></a>';
                                            }
                                        }

                                                echo '&nbsp';
                                            echo '</div>';
                                        if ($count == 0){
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>&nbsp';
                                        }
                                    }

                                    echo '</div>';
                                    if ($indeks > 3){
                                        //echo '<br/><a href="">See all...</a>';
                                        echo '<br/><a href="javascript:void()" onclick="goToFollowerUser(\''.$user.'\')">See all..<br/></a>';
                                    }

                            echo' </div>';
                        echo' </td>';
                    echo' </tr>';
                echo' </table>';
                echo' <div id="linkfromprofile">';

                        if (isset($_SESSION['username'])){
                                $login = true;
                        }
                        else{
                                $login = false;
                        }
                        $user = getUserNamePage();
                        if ($login){
                            if ($user == $_SESSION['username']){
                                echo '<div>';
                                //echo '    <a href="edit_profile.php?un='.$user.'">Edit Profile</a>';
                                echo '<div  class="button180" onclick="toEditProfile(\''.$user.'\')">Edit Profile</div>';
                                echo '</div>';
                            }
                        }
                        echo '<div>';
                            //echo '<a href="wall.php?'.$user.'">Wall</a>';
                            echo '<div  class="button180" onclick="toWall(\''.$user.'\')">Wall</div>';
                        echo '</div>';
                        echo '<div>';
                            //echo '<a href="fts.php?un='.$user.'">Photos</a>';
                            echo '<div  class="button180" onclick="toPhotos(\''.$user.'\')">Photos</div>';
                        echo '</div>';

                echo' </div>';
?>